package com.excel.annotation;

import com.excel.common.Constant;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import net.sf.cglib.beans.BeanMap;

public class AnnotationUtil implements Constant {
	public AnnotationUtil() {
	}

	public static Map<String, Object> getAnnotationTitleMap(Class<?> className)
			throws IllegalArgumentException, IllegalAccessException {
		Field[] filed = className.getDeclaredFields();
		int lenght_ = filed.length;
		Map<String, Object> value_Map = new LinkedHashMap<String, Object>(lenght_);
		if (filed != null && lenght_ > 0) {
			for (int i = 0; i < lenght_; ++i) {
				Field filed_ = filed[i];
				String filed_name = filed_.getName();
				ExcelAnnotation excleAn = (ExcelAnnotation) filed_.getAnnotation(ExcelAnnotation.class);
				if (excleAn != null) {
					value_Map.put(filed_name, excleAn.value());
				}
			}
		}

		return value_Map;
	}

	public static Map<String, Object> getTitleValueMap(Class<?> className)
			throws IllegalArgumentException, IllegalAccessException {
		Field[] filed = className.getDeclaredFields();
		int lenght_ = filed.length;
		Map<String, Object> value_Map = new LinkedHashMap<String, Object>(lenght_);
		if (filed != null && lenght_ > 0) {
			for (int i = 0; i < lenght_; ++i) {
				Field filed_ = filed[i];
				String filed_name = filed_.getName();
				ExcelAnnotation excleAn = (ExcelAnnotation) filed_.getAnnotation(ExcelAnnotation.class);
				if (excleAn != null) {
					value_Map.put(excleAn.value(), filed_name);
				}
			}
		}

		return value_Map;
	}

	public static Map<String, Object> getTitleType(Class<?> className)
			throws IllegalArgumentException, IllegalAccessException {
		Field[] filed = className.getDeclaredFields();
		int lenght_ = filed.length;
		Map<String, Object> value_Map = new LinkedHashMap<String, Object>(lenght_);
		if (filed != null && lenght_ > 0) {
			for (int i = 0; i < lenght_; ++i) {
				Field filed_ = filed[i];
				String filed_name = filed_.getName();
				ExcelAnnotation excleAn = (ExcelAnnotation) filed_.getAnnotation(ExcelAnnotation.class);
				if (excleAn != null) {
					value_Map.put(filed_name, excleAn.value());
				}
			}
		}

		return value_Map;
	}

	public static Object[] getAnnotationTitle(Class<?> className) throws IllegalArgumentException, IllegalAccessException {
		Field[] filed = className.getDeclaredFields();
		Set<String> set = new HashSet<String>(50);
		int lenght_ = filed.length;
		if (filed != null && lenght_ > 0) {
			for (int i = 0; i < lenght_; ++i) {
				Field filed_ = filed[i];
				String filed_name = filed_.getName();
				ExcelAnnotation excleAn = (ExcelAnnotation) filed_.getAnnotation(ExcelAnnotation.class);
				if (excleAn != null) {
					set.add(filed_name);
				}
			}
		}

		return set.toArray();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<BeanMap> getAnnotationData(Class<?> className, Collection<Object> bodyList)
			throws IllegalArgumentException, IllegalAccessException, InstantiationException {
		Object[] properties = getAnnotationTitle(className);
		List<BeanMap> bodyMap = new ArrayList<BeanMap>(bodyList.size());
		if (properties != null && properties.length != 0) {
			BeanMap targetBeanMap;
			if (bodyList != null && bodyList.size() > 0) {
				label50: for (Iterator<Object> var5 = bodyList.iterator(); var5.hasNext(); bodyMap.add(targetBeanMap)) {
					Object value = var5.next();
					targetBeanMap = BeanMap.create(className.newInstance());
					BeanMap srcBeanMap = BeanMap.create(value);
					srcBeanMap.setBean(value);
					Set<Entry> propertySet = srcBeanMap.entrySet();
					Object property = null;
					Iterator var11 = propertySet.iterator();

					while (true) {
						while (true) {
							Entry entry;
							do {
								do {
									if (!var11.hasNext()) {
										continue label50;
									}

									entry = (Entry) var11.next();
									property = entry.getKey();
								} while (properties == null);
							} while (properties.length <= 0);

							Object[] var15 = properties;
							int var14 = properties.length;

							for (int var13 = 0; var13 < var14; ++var13) {
								Object propertyName = var15[var13];
								if (property.equals((String) propertyName) && targetBeanMap.containsKey(property)) {
									targetBeanMap.put(property, entry.getValue());
									break;
								}
							}
						}
					}
				}
			}

			return bodyMap;
		} else {
			throw new IllegalArgumentException(className.getName() + " Does not contain ExcelAnnotation!");
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void copyProperties(Object target, Object source, String[] properties) {
		BeanMap targetBeanMap = BeanMap.create(target);
		BeanMap srcBeanMap = BeanMap.create(source);
		Set<Entry> propertySet = srcBeanMap.entrySet();
		Object property = null;
		Iterator var8 = propertySet.iterator();

		while (true) {
			while (true) {
				while (var8.hasNext()) {
					Entry entry = (Entry) var8.next();
					property = entry.getKey();
					if (properties != null && properties.length > 0) {
						String[] var12 = properties;
						int var11 = properties.length;

						for (int var10 = 0; var10 < var11; ++var10) {
							String propertyName = var12[var10];
							if (property.equals(propertyName) && targetBeanMap.containsKey(property)) {
								targetBeanMap.put(property, entry.getValue());
								break;
							}
						}
					} else if (targetBeanMap.containsKey(property)) {
						targetBeanMap.put(property, entry.getValue());
					}
				}

				return;
			}
		}
	}

	public static <T> T createBean(Class<T> clazz, Map<String, Object> srcMap, String[] tarProperties,
			String[] srcProperties) throws InstantiationException, IllegalAccessException {
		T t = clazz.newInstance();
		BeanMap beanMap = BeanMap.create(t);
		if (tarProperties != null && tarProperties.length > 0 && srcProperties != null && srcProperties.length > 0) {
			int index = 0;
			String[] var10 = tarProperties;
			int var9 = tarProperties.length;

			for (int var8 = 0; var8 < var9; ++var8) {
				String tarProperty = var10[var8];
				if (!tarProperty.contains(".")) {
					Object val = srcMap.get(srcProperties[index++]);
					beanMap.put(tarProperty, val);
				} else {
					String[] _tarProperties = tarProperty.split("\\.");
					int _index = 0;
					Object _obj = t;

					do {
						_obj = BeanMap.create(_obj).get(_tarProperties[_index++]);
					} while (_index < _tarProperties.length - 1);

					BeanMap.create(_obj).put(_tarProperties[_index], srcMap.get(srcProperties[index++]));
				}
			}
		}

		return t;
	}
}
